package edu.qc.cs370.project1;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;

import java.util.*;

public class Gui
{

	JFrame jf = new JFrame("Black Jack Game");
	Container myCP = jf.getContentPane();
	JTextArea [] text;
	Storage storage;
	Border border = BorderFactory.createEtchedBorder(Color.GREEN, Color.WHITE);

	public Gui()
	{
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setLocation(0, 0);
		jf.setSize(3000, 250);
		 
	//should not have 2 storage objects//	storage = new Storage();
	}

	public void setStorage(Storage s)
	{
		storage = s;
	}

	public int askTotalNumberOfPlayers()
	{
		Integer[] options ={1,2,3,4,5};
		int select = (Integer)JOptionPane.showInputDialog(null, "How many players total?",
					"Number of players", JOptionPane.QUESTION_MESSAGE, null, options, null);
		text = new JTextArea[7];
		return select;	
	}// askTotalNumberOfPlayers

	public int askNumberOfHumanPlayers()
	{
		int num=storage.getNumberOfPlayers();
		Integer[] options =new Integer[num];
		for(int i=0;i<num;i++)
			options[i]=i+1;
		int select = (Integer)JOptionPane.showInputDialog(null, "How many of those players are human?",
			"Number of players", JOptionPane.QUESTION_MESSAGE, null, options, null);

		return select;
	}// askNumberOfHumanPlayers

	public String getPlayerName()
	{
		String name = JOptionPane.showInputDialog(null, "Please enter a player's name.");
		while (name == null || name.trim().length() == 0)
		{
			name = JOptionPane.showInputDialog(null, "Player's name may not be empty. \nPlease enter a player's name.");
		}
		
		return name;
	}// getPlayerName

	public boolean askPlayerToHit(Player p)
	{
		String hand = "";
		ArrayList<Card> hand2 = p.showHand();
		for(int j = 0; j < hand2.size(); j++)
		{
			hand += (hand2.get(j).getName() + " \n");
		}
		
		int decision = JOptionPane.showConfirmDialog(null, "Player " + p.getName() + ":\n\n Your current hand is:\n" + hand +
				"\n Your current score is: " + p.getScore() + "\n\nWould you like to hit?", "Ask Player to Hit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

		if (decision == 0)
			return true;
		else
			return false;
	}// askPlayerToHit

	public boolean askPlayerToContinue()
	{
		int decision = JOptionPane.showConfirmDialog(null, "Would all of you like to Continue the game?",
				"Continue Game", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

		if (decision == 0)
			return true;
		else
			return false;
	}// boolean askPlayerToContinue

	public void refreshDisplay()
	{
		myCP.removeAll();
		myCP.setLayout(new GridLayout(1,7));
		int k = 0;
		
		while(k < storage.getNumberOfPlayers())
		{
			if(storage.getPlayers().get(k).isStillIn())
				break;
			k++;
		}

		for(int i = 0; i < storage.getNumberOfPlayers(); i++)
		{
			Player p = storage.getPlayers().get(i);
			String name = p.getName();
			String hand = "";
			ArrayList<Card> hand2 = p.showHand();
			
			if(k == storage.getNumberOfPlayers())
				hand += hand2.get(0).getName() + "\n";
			for(int j = 1; j < hand2.size(); j++)
			{
				hand += (hand2.get(j).getName() + " \n");
			}
			int money = p.getMoney();
			int score;			
			if(k == storage.getNumberOfPlayers())
				score = p.getScore();
			else if(storage.getPlayers().get(i).getScore() == 0)
				score = 0;
			else
				score = p.getScore()-p.showHand().get(0).getValue();
			if(k == storage.getNumberOfPlayers())
				text[i] = new JTextArea("Player: "+name + " \n\nHand:\n"+hand + "\nScore: " + Integer.toString(score)+ "\n\nCurrent Balance: $" + Integer.toString(money) + "  \n");
			else
				text[i] = new JTextArea("Player: "+name + " \n\nHand:\n"+hand + "\n\nCurrent Balance: $" + Integer.toString(money) + "  \n");
			text[i].setBorder(border);
			text[i].setBackground(Color.PINK);
			myCP.add(text[i]);
		}//for

		String name = storage.getDealer().getName();
		String hand = "";
		ArrayList<Card> hand2 = storage.getDealer().showHand();
		
		if(storage.getDealer().isStillIn() == false)
			hand += hand2.get(0).getName() + "\n";

		for(int j = 1; j < hand2.size(); j++)
		{
			hand += (hand2.get(j).getName() + " \n");
		}
		int score;
		if(k == storage.getNumberOfPlayers())
			score = storage.getDealer().getScore();
		else if(storage.getDealer().getScore() == 0)
			score = 0;
		else
			score = storage.getDealer().getScore()-storage.getDealer().showHand().get(0).getValue();
		if(k == storage.getNumberOfPlayers())
			text[text.length-2] = new JTextArea("Player: "+name + "\n\nHand:\n" + hand + "\n" + "Score: " + Integer.toString(score));
		else
			text[text.length-2] = new JTextArea("Player: "+name + "\n\nHand:\n" + hand+"\n\n");
		text[text.length-2].setBorder(border);
		text[text.length-2].setBackground(Color.PINK);
		myCP.add(text[text.length-2]);
		
		text[text.length-1] = new JTextArea("Pot Money: $"+Integer.toString(storage.getPot()));
		text[text.length-1].setBorder(border);
		text[text.length-1].setBackground(Color.PINK);
		myCP.add(text[text.length-1]);

		jf.pack();
		jf.setVisible(true);
	}// refreshDisplay

	public void displayMessage(String message)
	{
		myCP.remove(text[text.length-1]);
		text[text.length-1] = new JTextArea("Pot Money: $"+Integer.toString(storage.getPot()) + "\n\n\n" + message);
		text[text.length-1].setBorder(border);
		myCP.add(text[text.length-1]);
		text[text.length-1].setBackground(Color.PINK);
		jf.pack();
		jf.setVisible(true);
	}// displayMessage
}// GUI
